package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.LinkageConfigService;
import com.xdtech.hotel.vo.LinkageConfigItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-23 20:46:32
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class LinkageConfigController {
	@Autowired
	private LinkageConfigService linkageConfigService;
	@RequestMapping(value="/linkageConfig.do",params = "linkageConfig")
	public ModelAndView skipLinkageConfig() {
		return new ModelAndView("hotel/linkageConfig/linkageConfig_ftl");
	}
	
	@RequestMapping(value="/linkageConfig.do",params = "skip")
	public ModelAndView skip(String page) {
		return new ModelAndView("hotel/linkageConfig/"+page+"_ftl");
	}
	
	@RequestMapping(value="/linkageConfig.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(LinkageConfigItem item,Pagination pg) {
		Map<String, Object> results =  linkageConfigService.loadPageDataByConditions(pg,item,"findLinkageConfigByCondition");
		return results;
	}
	
	@RequestMapping(value="/linkageConfig.do",params = "editLinkageConfig")
	public ModelAndView editLinkageConfig(HttpServletRequest request,Long linkageConfigId) {
		if (linkageConfigId!=null) {
			request.setAttribute("linkageConfigItem", linkageConfigService.loadLinkageConfigItem(linkageConfigId));
		}
		return new ModelAndView("hotel/linkageConfig/editLinkageConfig_ftl");
	}
	
	@RequestMapping(value="/linkageConfig.do",params = "saveLinkageConfig")
	@ResponseBody
	public ResultMessage saveLinkageConfig(LinkageConfigItem item) {
		ResultMessage r = new ResultMessage();
		if (linkageConfigService.saveOrUpdateLinkageConfig(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/linkageConfig.do",params = "deleteLinkageConfigItems")
	@ResponseBody
	public ResultMessage deleteLinkageConfigItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> linkageConfigIds = new ArrayList<Long>();
			for (String id : tempIds) {
				linkageConfigIds.add(Long.valueOf(id));
			}
			linkageConfigService.deleteLinkageConfigInfo(linkageConfigIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
